home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Language / Compiler / Builtins / ImmutableVector.m < prev    next >
Encoding:
Text File  |  1990-08-16  |  3.2 KB  |  109 lines

  1. % @(#)ImmutableVector.X    1.2  4/11/88
  2. %
  3. export _immutableVectorObject to "Builtins"
  4.  
  5. const _immutableVectorObject == 
  6.   immutable object _immutableVectorObject
  7.     export of
  8.     function of [ElementType : AbstractType] -> [result : NIMVT]
  9.       where
  10.     NIMVT ==    immutable type NIMVT
  11.           operation create [Integer] -> [NIMV]
  12.           operation new [RIS] -> [NIMV]
  13.           function getSignature -> [Signature]
  14.         end NIMVT
  15.     NIMV ==    immutable type NIMV
  16.           operation hiddenSetElement[Integer, ElementType]
  17.           function  getElement [Integer] -> [ElementType]
  18.             % get the element indexed by index, failing if index 
  19.             % out of range.
  20.           function  upperbound -> [Integer]
  21.             % return the highest valid index, ub.
  22.           function  lowerbound -> [Integer]
  23.             % return the lowest valid index, always 0.
  24.           function  getSlice [Integer, Integer] -> [NIMV]
  25.             % See implementation comment
  26.           operation catenate [a : NIMV] -> [r : NIMV]
  27.             % return a new vector the result of catenating the 
  28.             % elements of a to self
  29.         end NIMV
  30.     RIS ==  type RIS
  31.           function  getElement [Integer] -> [ElementType]
  32.           function  upperbound -> [Integer]
  33.           function  lowerbound -> [Integer]
  34.         end RIS
  35.     ElementType *> type T end T
  36.       end where
  37.       result <- 
  38.     immutable object aNIMVT
  39.       export create, new, getSignature
  40.  
  41.       function getSignature -> [result : Signature]
  42.         result <- NIMV
  43.       end getSignature
  44.  
  45.       operation create[length : Integer] -> [result : NIMV]
  46.         result <-
  47.           immutable object aNIMV
  48.         export getElement, hiddenSetElement, upperbound, lowerbound,
  49.               getSlice, catenate
  50.  
  51.         initially
  52.           var x : Integer <- length
  53.         end initially
  54.  
  55.         function  getElement [index : Integer] -> [result : ElementType]
  56.           % get the element indexed by index, failing if index 
  57.           % out of range.
  58.           primitive 012 [result] <- [index]
  59.         end getElement
  60.         operation hiddenSetElement [index : Integer, e : ElementType]
  61.           % set the element, failing if index out of range
  62.           primitive 112 [] <- [index, e]
  63.         end hiddenSetElement
  64.         function  upperbound -> [r : Integer]
  65.           % return the highest valid index, ub.
  66.           primitive 212 [r] <- []
  67.         end upperbound
  68.         function  lowerbound -> [r : Integer]
  69.           % return the lowest valid index, always 1.
  70.           primitive 312 [r] <- []
  71.         end lowerbound
  72.         function  getSlice [i1 : Integer, length : Integer] -> [r : NIMV]
  73.           % return a new Vector, a, with lower bound 0, and 
  74.           % upper bound length-1, such that for 0 <= i < length:
  75.           %     self.getElement[i1+i] == a.getElement[i]
  76.           % fail if i1 or i1+length is out of range.
  77.           primitive 412 [r] <- [i1, length]
  78.         end getSlice
  79.         operation catenate [a : NIMV] -> [r : NIMV]
  80.           % return a new vector the result of catenating the 
  81.           % elements of a to self
  82.           primitive 612 [r] <- [a]
  83.         end catenate
  84.           end aNIMV
  85.       end create
  86.       operation new[value : RIS] -> [r : NIMV]
  87.         var i    : Integer
  88.         var j     : Integer
  89.         var limit    : Integer
  90.         var e    : ElementType
  91.  
  92.         i <- value.lowerbound
  93.         limit <- value.upperbound
  94.         r <- self.create[limit - i + 1]
  95.         
  96.         j <- 0
  97.         loop
  98.           exit when i > limit
  99.           e <- value(i)
  100.           r.hiddenSetElement[j, e]
  101.           i <- i + 1
  102.           j <- j + 1
  103.         end loop
  104.       end new
  105.     end aNIMVT
  106.     end of
  107.   end _immutableVectorObject
  108.